//
//  NXEighthViewController.m
//  BiologyLibrary
//
//  Created by milo on 16/7/5.
//  Copyright © 2016年 youxh. All rights reserved.
//

#import "NXEighthViewController.h"
#import "NXNinthStepViewController.h"
@interface NXEighthViewController ()
{
    NSMutableArray *typeArray;
}
@end

@implementation NXEighthViewController

static const NSString * KEY_STEP  = @"step8";


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    typeArray = [NSMutableArray arrayWithCapacity:0];
    _currentStep = self.NewRecordArray.firstObject.savedStepCount + 1;
    _keyStepStr = [NSString stringWithFormat:@"step%ld", _currentStep];
    NSLog(@"setp%ld record === %@", _currentStep, self.NewRecordArray.firstObject.getDetailDict);
    
    [self setNaviBarTitle:self.NewRecordArray.firstObject.name];
    _maintableview=[[UITableView alloc]initWithFrame:CGRectMake(10, 64, fWidth_Current-20, fHeight_Current-64-50)];
    _maintableview.backgroundColor = [UIColor clearColor];
    _maintableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _maintableview.dataSource = self;
    _maintableview.delegate = self;
    [self.view addSubview:_maintableview];
    titlestr=@"7、组织样本自离体至置入液氮或福尔马林时间";
    typeArray = self.types;
   
    
    
    
    UIImageView*buttomview=[[UIImageView alloc]initWithFrame:CGRectMake(0, fHeight_Current-50, fWidth_Current, 50)];
    buttomview.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
    [self.view addSubview:buttomview];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 50)];
    label.backgroundColor=[UIColor clearColor];
    label.textAlignment=1;
    label.font=[UIFont systemFontOfSize:14];
    label.textColor=[UIColor lightGrayColor];
    label.text= [NSString stringWithFormat:@"当下是第%ld/%ld步",
                 (long)self.NewRecordArray.firstObject.savedStepCount + 1, (long)self.NewRecordArray.firstObject.allStepCount];
    [buttomview addSubview:label];
    
}
#pragma mark - UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 2+typeArray.count;
        
    }else{
        return 1;
        
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
    
        return 2;//返回标题数组中元素的个数来确定分区的个数
    }
    
    return 4;//返回标题数组中元素的个数来确定分区的个数
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        if (indexPath.row==0) {

            CGSize hight=[TGCDataCenter getContentSize:titlestr WithMaxSize:CGSizeMake(fWidth_Current-40, 1000) Font:[UIFont systemFontOfSize:17] LineBreakMode:NSLineBreakByWordWrapping];
            
            return hight.height+40;
        }else if(indexPath.row>0&&indexPath.row<typeArray.count+1)
        {
            return 40;
        }else
        {
            return 30;
        }
    }else{
        return 50;
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView*headview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 18)];
    headview.backgroundColor=[UIColor clearColor];
    return headview;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *strIdentifier = @"strIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
    
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                      reuseIdentifier:strIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
        
    }
    switch (indexPath.section) {
        case 0:
            if (indexPath.row==0) {
                CGSize hight=[TGCDataCenter getContentSize:titlestr WithMaxSize:CGSizeMake(fWidth_Current-40, 1000) Font:[UIFont systemFontOfSize:17] LineBreakMode:NSLineBreakByWordWrapping];
                UILabel *titlelab=[[UILabel alloc]initWithFrame:CGRectMake(10, 20, fWidth_Current-40, hight.height)];
                titlelab.textColor=[UIColor whiteColor];
                titlelab.backgroundColor=[UIColor clearColor];
                titlelab.text=titlestr;
                titlelab.numberOfLines=0;
                titlelab.font=[UIFont systemFontOfSize:17];
                
                
                UIImageView*lineview=[[UIImageView alloc]initWithFrame:CGRectMake(0, hight.height+40, fWidth_Current-20, 1)];
                lineview.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:0.3];
                [cell addSubview:titlelab];
                [cell addSubview:lineview];
            }else if(indexPath.row>0&&indexPath.row<typeArray.count+1)
            {
                NSString*titlestr=[NSString stringWithFormat:@"样本号：%@",[self.NewRecordArray objectAtIndex:indexPath.row-1].recordId];
                cell.textLabel.text = titlestr;
                cell.textLabel.textColor=cell.detailTextLabel.textColor=[UIColor whiteColor];
                if ([typeArray[indexPath.row-1] isEqualToString:@"0"]) {
                    cell.detailTextLabel.text=@"小于20分钟";
                }else{
                    cell.detailTextLabel.text=@"大于20分钟";
                    
                }
                UIImageView*lineview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 39, fWidth_Current-20, 1)];
                lineview.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:0.3];
                [cell addSubview:lineview];
            }else
            {
                UILabel*tishilab=[[UILabel alloc]initWithFrame:CGRectMake(30, 0, fWidth_Current-80, 30)];
                tishilab.text=@"注意：结果由软件自行判断完成";
                tishilab.textColor=[UIColor grayColor];
                tishilab.backgroundColor=[UIColor clearColor];
                tishilab.font=[UIFont systemFontOfSize:12];
                [cell addSubview:tishilab];
            }
            break;
        case 1:{
            cell.backgroundColor=[UIColor clearColor];
            
            if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
                UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, (fWidth_Current-20), 50)];
                btn1.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
                btn1.layer.cornerRadius=5;
                [btn1 setTitle:@"下一步" forState:UIControlStateNormal];
                btn1.clipsToBounds=YES;
                [btn1 addTarget:self action:@selector(nextStepClick) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn1];
            }else{
                UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, (fWidth_Current-20), 50)];
                btn1.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
                btn1.layer.cornerRadius=5;
                [btn1 setTitle:@"取消" forState:UIControlStateNormal];
                btn1.clipsToBounds=YES;
                [btn1 addTarget:self action:@selector(cancelclick) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn1];
            }

        }
            break;
        case 2:{
            UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, (fWidth_Current-20), 50)];
            btn2.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
            btn2.layer.cornerRadius=5;
            [btn2 setTitle:@"保存" forState:UIControlStateNormal];
            btn2.clipsToBounds=YES;
            [btn2 addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:btn2];

        }
            break;
        case 3:{
            UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, (fWidth_Current-20), 50)];
            btn2.backgroundColor=[UIColor colorWithRed:0.94 green:0.23 blue:0.38 alpha:1.0];
            btn2.layer.cornerRadius=5;
            [btn2 setTitle:@"暂存" forState:UIControlStateNormal];
            btn2.clipsToBounds=YES;
            [btn2 addTarget:self action:@selector(zancunclick) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:btn2];
        }
            break;
        default:
            break;
    }
    return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==3) {
        return 70.0f;
        
    }else{
        return 0;
    }
}

- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section==3) {
        UIView *footerView = [[UIView alloc] initWithFrame:
                              CGRectMake(0, 0, tableView.frame.size.width , 50)];
        footerView.backgroundColor = [UIColor clearColor];
        UILabel*tishilab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current-80, 30)];
        tishilab.text=@"提示：暂存之后信息将无法修改";
        tishilab.textColor=[UIColor grayColor];
        tishilab.backgroundColor=[UIColor clearColor];
        tishilab.font=[UIFont systemFontOfSize:12];
        [footerView addSubview:tishilab];
        return footerView;
        
    }else{
        UIView *footerView = [[UIView alloc] initWithFrame:
                              CGRectMake(0, 0, tableView.frame.size.width , 10)];
        footerView.backgroundColor = [UIColor clearColor];
        return footerView;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)zancunclick{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"暂存" message:@"暂存之后信息将无法修改" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    alert.delegate=self;
    alert.tag=1;
    [alert show];
    
    
}
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag==1) {
        if (buttonIndex==1) {
            for (Record *model in self.NewRecordArray) {
                model.savedStepCount = _currentStep;
                model.recordStatus = STATUS_RECORD_SAVED;

                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    [model update];
                });
            }
     
            NXNinthStepViewController *NXNinthStepVC = [[NXNinthStepViewController alloc] init];
            NXNinthStepVC.NewRecordArray = self.NewRecordArray;
            [self.navigationController pushViewController:NXNinthStepVC animated:YES];
        }
    }
}


-(void)saveClick{
    for (Record *model in self.NewRecordArray) {
        model.savedStepCount = _currentStep;
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [model update];
        });
    }

    NXNinthStepViewController *NXNinthStepVC = [[NXNinthStepViewController alloc] init];
    NXNinthStepVC.NewRecordArray = self.NewRecordArray;
    [self.navigationController pushViewController:NXNinthStepVC animated:YES];
}
-(void)cancelclick{
    UIViewController*miloViewControl=(UIViewController *)[[self.navigationController viewControllers]objectAtIndex:1];
    [self.navigationController popToViewController:miloViewControl animated:YES];
}
-(void)creatDetailDict{
    
   
}

-(void)nextStepClick{
    for (Record *model in self.NewRecordArray) {
        model.savedStepCount = _currentStep;
    }

    NXNinthStepViewController *NXNinthStepVC = [[NXNinthStepViewController alloc] init];
    NXNinthStepVC.NewRecordArray = self.NewRecordArray;
    NXNinthStepVC.fromtype = @"search";
    [self.navigationController pushViewController:NXNinthStepVC animated:YES];
}

@end
